<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\web\IdentityInterface;

/**
 * This is the model class for table "user".
 *
 * @property int $id id
 * @property string|null $username 用户名
 * @property string|null $nickname 昵称
 * @property string|null $password 密码
 * @property string|null $head_pic_url 头像地址
 * @property int|null $sex 性别
 * @property string|null $email 邮箱
 * @property string|null $phone 手机号
 * @property string|null $wx_mp_openid 微信小程序openid
 * @property string $access_token 登录token(接口使用)
 * @property integer $created_at 创建时间
 * @property integer $updated_at 修改时间
 */
class User extends \yii\db\ActiveRecord implements IdentityInterface
{

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class
            ]
        ]; // TODO: Change the autogenerated stub
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['sex'], 'integer'],
            [['username', 'nickname', 'password', 'head_pic_url', 'email', 'phone','wx_mp_openid', 'access_token'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'username' => Yii::t('app', 'Username'),
            'nickname' => Yii::t('app', 'Nickname'),
            'password' => Yii::t('app', 'Password'),
            'head_pic_url' => Yii::t('app', 'Head Pic Url'),
            'sex' => Yii::t('app', 'Sex'),
            'email' => Yii::t('app', 'Email'),
            'phone' => Yii::t('app', 'Phone'),
            'wx_mp_openid'=>Yii::t('app','Wx Mp Openid'),
            'access_token' => Yii::t('app', 'Access Token'),
            'created_at' => Yii::t('app', 'Created At'),
            'update_at' => Yii::t('app', 'Updated At'),
        ];
    }

    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        return static::find()
            ->where([ '=','access_token', $token])
            ->one();
    }

    public static function findByMpOpenid($openid)
    {
        $model = static::find()
            ->where(['=','wx_mp_openid',$openid])
            ->one();
        if(!$model){
            $model = new static();
            $model->wx_mp_openid = $openid;
            $model->save();
        }

        return $model;
    }

    public function validatePassword($password){
        return $this->password == $password;
    }

    public function login(){
        $this->generateAccessToken();
        $this->save();
        return $this;
    }

    public function generateAccessToken()
    {
        $this->access_token = Yii::$app->security->generateRandomString();
    }

    public function getId()
    {
        return $this->id;
    }

    public function getAuthKey()
    {
        // TODO: Implement getAuthKey() method.
    }

    public function validateAuthKey($authKey)
    {
        // TODO: Implement validateAuthKey() method.
    }

    public function fields()
    {
        return array_merge(parent::fields(),[
            'head_pic_url'=>function(){
                return $this->head_pic_url?$this->head_pic_url:'https://randomuser.me/api/portraits/men/81.jpg';
            }
        ]); // TODO: Change the autogenerated stub
    }
}
